<?php


	class groupAction extends userAction{
		private $feedSetting = false; //群组操作是否加入动态
		protected function _initialize() {
			parent::_initialize();
			$this->feedSetting = getAddFeedSetting($this->userInfo['id'],"groupaction"); 
		}

		public function index(){
			isset($_GET['u']) ? $u = safeRequest($_GET['u'],1) : $u = "all";
			$dao = D("groups_topics");
			$fields = "id,groupid,userid,title,lastuserid,lastreverttime,revertnum";
			if( 'all' == $u ){
				//$groupIds = F("groupId",'',DATA_PATH.$this->userInfo['id']);
				//$group = D('groups_users');
				//$groupuser = $group->findAll('userid='.$this->userInfo['id'],'groupid','groupid asc');
				$groupuser = F("userGroup","",-1,getCachePath(APP_PATH."/UserCache",$this->userInfo['id']));
				$groupArray = array();
				foreach($groupuser as $item){
					$groupArray[] = $item['groupid'];
				}
				$groupIds = implode(',',$groupArray);
				$list = $dao->findAll("groupid in ($groupIds)",$fields,'lastreverttime desc','20');
			}else{
				$this->rank =  getRank($this->userInfo['id'],$u,"group");
				if(!$this->rank){
					redirect(__APP__."/rank/norank/u/".$u);
					return false;
				}
				$list = $dao->findAll('userid='.$u,$fields,'lastreverttime desc','20');
			}
			$this->assign("list",$list);
			$this->display();
		}

		//群组列表
		public function group(){
			import("ORG.Util.Page");
			isset($_GET['u']) ? $u = safeRequest($_GET['u'],1) : $u = "all";
			isset($_GET["p"]) ? $p = safeRequest($_GET["p"]) : $p = 1;
			$dao = D('groups');
			if( 'all' == $u ){
				$groupCount = $dao->count("","id"); 
				$p = new Page($groupCount,"20"); 
				$list = $dao->findAll('','*','id asc',$p->firstRow.','.$p->listRows);
				$page  = $p->show();
				$this->assign('grouplist',$list);
				$this->assign("page",$page);
				$this->assign("groupcount",$groupCount);
				$this->display('allgroup');
			}else{
				$this->rank =  getRank($this->userInfo['id'],$u,"group");
				if(!$this->rank){
					redirect(__APP__."/rank/norank/u/".$u);
					return false;
				}
				$groupuser = F("userGroup","",-1,getCachePath(APP_PATH."/UserCache",$this->userInfo['id']));
				$groupArray = array();
				foreach($groupuser as $item){
					$groupArray[] = $item['groupid'];	
				}
				$groupIds = implode(',',$groupArray);
				$groupCount = $dao->count("id in ($groupIds)","id"); 
				$p = new Page($groupCount,"20"); 
				$list = $dao->findAll("id in ($groupIds)","*","lastreverttime desc",$p->firstRow.','.$p->listRows);
				$page  = $p->show();
				$this->assign('grouplist',$list);
				$this->assign("page",$page);
				$this->assign("groupcount",$groupCount);
				$this->assign("u",$u);
				$this->display();
			}

		}

		//主题列表
		public function showboard(){
			$groupid = safeRequest($_GET['id']);
			$groupInfo = D('groups')->find($groupid);
			$isManager = 0;
			$groupuserDao = D('groups_users');
			$groupuserInfo = $groupuserDao->find("groupid=".$groupid." and userid=".$this->userInfo['id']);
			if($groupuserInfo){
				if($groupuserInfo['usertype'] == 0){
					$isManager = 1; //群组成员
				}elseif($groupuserInfo['usertype'] == 1){
					$isManager = 2; //群组管理员
				}elseif($groupuserInfo['usertype'] == 2){
					$isManager = 3; //群组创建人
				}
			}
			
			if($groupInfo['setting'] == 2 && $isManager == 0){
				$this->error(L("no_rank"));
			}else{
				if($groupInfo['setting'] ==1 && $isManager == 0){
					$list = "noAccess";
				}else{
					import("ORG.Util.Page"); //导入分页类
					$dao = D('groups_topics');
					isset($_GET["p"]) ? $p = safeRequest($_GET["p"]) : $p = 1;
					$topicCount = $dao->count('groupid='.$groupid,'id'); 
					$listRows = C("topicListRows"); 
					$p = new Page($topicCount,$listRows); //实例化分页类
					$p->setConfig('header' ,L("page_title")); 
					$list = $dao->findAll('groupid='.$groupid,'id,groupid,userid,regtime,title,lastuserid,lastreverttime','istop desc, lastreverttime desc',$p->firstRow.','.$p->listRows);
					$page  = $p->show();
					$this->assign("page",$page); //绑定分页
				}
			}
			$this->assign('group',$groupInfo);
			$this->assign('list',$list);
			$this->assign('isManager',$isManager);
			$this->display();
		}

		//显示主题
		public function showtopic(){
			import("ORG.Util.Page"); //导入分页类
			//获得相关ID的主题
			$id = safeRequest($_GET['id']);
			$dao = D('groups_topics');
			$record = $dao->find($id);
			if($record){
				//主题所属群组信息
				$groupInfo = D('groups')->find($record['groupid']);
				$isManager = 0;
				$groupuserDao = D('groups_users');
				$groupuserInfo = $groupuserDao->find("groupid=".$record['groupid']." and userid=".$this->userInfo['id']);
				if($groupuserInfo){
					if($groupuserInfo['usertype'] == 0){
						$isManager = 1; //群组成员
					}elseif($groupuserInfo['usertype'] == 1){
						$isManager = 2; //群组管理员
					}elseif($groupuserInfo['usertype'] == 2){
						$isManager = 3; //群组创建人
					}
				}
				if($groupInfo['setting'] > 0 && $isManager == 0){
					$this->error(L("no_rank"));
				}else{
					//该主题评论
					//$revertCount = D('groups_topics_reverts')->count('topicid='.$id,'id');
					$revertCount = $record['revertnum'];
					$listRows = C("topicListRows"); 
					$p = new Page($revertCount,$listRows); //实例化分页类
					$p->setConfig('header' ,L("revert_page_title")); 
					$topicRevert = D('grouptopicrevertview')->findAll('groups_topics_reverts.topicid='.$id.' and groups_topics_reverts.userid=user.id','*','groups_topics_reverts.id asc',$p->firstRow.','.$p->listRows);
					$page  = $p->show();
					$this->assign('topic',$record);
					$this->assign('group',$groupInfo);
					$this->assign('revert',$topicRevert);
					$this->assign('isManager',$isManager);
					$this->assign("page",$page); 
					$this->display();
				}
			}else{
				$this->error(L("no_record"));
			}
		}

		//添加主题入库
		public function savetopic(){
			$groupid = safeRequest($_POST["groupid"]);
			if(D("groups_users")->find("groupid=".$groupid." and userid=".$this->userInfo['id'])){
				$dao = D('groups_topics');
				if($dao->create()){
					$dao->userid = $this->userInfo['id'];
					$dao->regtime = time();
					$dao->lastuserid = $this->userInfo['id'];
					$dao->lastreverttime = time();
					$id = $dao->add();
					if($id>0){
						//判断是否添加动态
						if( $this->feedSetting ){
							addFeed($this->userInfo['id'],'groups_topics','发表新主题',$id);
						}
						D("groups")->setField("topicnum","(topicnum+1)","id=".$groupid);
						$this->success(L("add_topic_success"));
					}else{
						$this->error(L("add_topic_error"));
					}
				}else{
					$this->error($dao->getError());
				}
			}else{
				$this->error(L("no_rank"));
			}
		}

		//预留的非ajax方式编辑主题
		public function edittopic(){
			$id = safeRequest($_POST["id"]);
			$record = D("groups_topics")->find("id=".$id." and userid=".$this->userInfo['id']);
			if($record){
				$this->assign("topic",$record);
				$this->display();
			}else{
				$this->error(L("no_rank"));
			}
		}

		//更新主题
		public function updatetopic(){
			$id = safeRequest($_POST["id"]);
			$dao = D("groups_topics");
			$dao->find("id=".$id." and userid=".$this->userInfo['id']);
			$dao->title = str_filter($_POST["title"]);
			$dao->content = str_filter($_POST["content"]);
			if($dao->save()){
				isset($_POST["ajax"]) ? print "success" : $this->success(L("edit_topic_success"));
			}else{
				isset($_POST["ajax"]) ? print "error" : $this->error(L("edit_topic_error"));
			}
		}

		//置顶主题
		public function toptopic(){
			$groupid = safeRequest($_GET["gid"]);
			$topicid = safeRequest($_GET["id"]);
			if( D( "groups_users" )->find( "groupid=".$groupid ." and userid=".$this->userInfo['id']." and usertype>0" ) ){
				if(D("groups_topics")->setField("istop",1,"groupid =".$groupid." and id=".$topicid)){
					isset($_GET["ajax"]) ? print "success" : $this->success(L("top_topic_success"));
				}else{
					isset($_GET["ajax"]) ? print "error" : $this->error(L("top_topic_error"));
				}
			}else{
				isset($_GET["ajax"]) ? print "error" : $this->success(L("no_rank"));
			}
		}

		//取消置顶
		public function untoptopic(){
			$groupid = safeRequest($_GET["gid"]);
			$topicid = safeRequest($_GET["id"]);
			if(D( "groups_users")->find("groupid=".$groupid ." and userid=".$this->userInfo['id']." and usertype>0") ){
				if(D("groups_topics")->setField("istop",0,"groupid =".$groupid." and id=".$topicid)){
					isset($_GET["ajax"]) ? print "success" : $this->success(L("untop_topic_success"));
				}else{
					isset($_GET["ajax"]) ? print "error" : $this->error(L("untop_topic_error"));
				}
			}else{
				isset($_GET["ajax"]) ? print "error" : $this->success(L("no_rank"));
			}
		}

		//删除主题
		public function deletetopic(){
			//$groupid = safeRequest($_GET["gid"]);
			$topicid = safeRequest($_GET["id"]);
			$dao = D("groups_topics");
			$record = $dao->find("id=".$topicid,"groupid");
			if($record){
				if($dao->delete("id=".$topicid." and userid=".$this->userInfo['id'])){//本人删除
					D("groups_topics_reverts")->delete("topicid=".$topicid);//删除评论
					$this->success(L("delete_topic_success"));
				}else{
					if(D("groups_users")->find("groupid=".$record['groupid']." and userid=".$this->userInfo['id']." and usertype>0")){//是否管理员
						if($dao->delete($topicid)){
							D("groups_topics_reverts")->delete("topicid=".$topicid);//删除评论
							D("groups")->setField("topicnum","(topicnum-1)","id=".$record->groupid);
							//$this->success("删除主题成功");
							$this->redirect('group','group','',APP_NAME,'',3,L("delete_topic_success"));
						}else{
							$this->error(L("delete_topic_error"));
						}
					}else{
						$this->error(L("no_rank"));
					}
				}
			}else{
				$this->error(L("no_record"));
			}
		}
		
		//保存回复
		public function saverevert(){
			$groupid = safeRequest($_POST["groupid"]);
			$dao = D('groups_topics_reverts');
			if(D("groups_users")->find("groupid=".$groupid." and userid=".$this->userInfo['id'])){
				if($dao->create()){
					$dao->userid = $this->userInfo['id'];
					$dao->reverttime = time();
					$id = $dao->add();
					if($id>0){
						$tdao = D('groups_topics');
						$tdao->find("id=".safeRequest($_POST['topicid']),"id,userid,lastuserid,lastreverttime,revertnum");
						$tdao->lastuserid = $this->userInfo['id'];
						$tdao->lastreverttime = time();
						$tdao->revertnum = '(revertnum+1)';
						$tdao->save();
						//添加通知
						if($tdao['userid'] != $this->userInfo['id']){
							addNotify($tdao['userid'],$this->userInfo['id'],"topicrevert",$tdao['id'],$id);
						}
						if($_POST["reuserid"]>0 && $_POST["reuserid"]!=$this->userInfo['id']){
							addNotify($_POST["reuserid"],$this->userInfo['id'],"topicrevert2",$tdao['id'],$id);
						}
						$this->success(L("revert_topic_success"));
					}
				}else{
					$this->error($dao->getError());
				}
			}else{
				$this->error(L("no_rank"));
			}
		}
		
		//预留的非ajax方式编辑回复
		public function editrevert(){
			$id = safeRequest($_GET['id']);
			$record = D("groups_topics_reverts")->find("id=".$id." and userid=".$this->userInfo['id']);
			if($record){
				$this->assign("record",$record);
				$this->display();
			}else{
				$this->error(L("no_record"));
			}
		}
		
		//更新回复
		public function updaterevert(){
			$rid = safeRequest($_POST['rid']);
			$dao = D("groups_topics_reverts");
			if($dao->find("id=".$rid." and userid=".$this->userInfo['id'])){
				$dao->content = str_filter($_POST['content']);
				$dao->save();
				isset($_POST['ajax']) ? print "success" : $this->success(L("edit_revert_success"));
			}else{
				isset($_POST['ajax']) ? print "error" : $this->error(L("edit_revert_error"));
				
			}
		}
		

		//删除回复
		public function deleterevert(){
			$rid = safeRequest($_GET['id']);		
			$dao = D("groups_topics_reverts");
			$record = $dao->find("id=".$rid." and userid=".$this->userInfo['id']);
			if($record){
				if($dao->delete("id=".$rid." and userid=".$this->userInfo['id'])){
					D("groups_topics")->setField("revertnum","(revertnum-1)","id=".$record['topicid']);
					isset($_GET['ajax']) ? print "success" : $this->success(L("delete_revert_success"));
				}else{
					if(D("groups_users")->find("groupid=".$record['groupid']." and userid=".$this->userInfo['id']." and usertype>0")){//是否管理员
						if($dao->delete($rid)){
							D("groups_topics")->setField("revertnum","(revertnum-1)","id=".$record['topicid']);
							isset($_GET['ajax']) ? print "success" : $this->success(L("delete_revert_success"));
						}else{
							isset($_GET['ajax']) ? print "error" : $this->error(L("delete_revert_error"));
						}
					}else{
						isset($_GET['ajax']) ? print "error" : $this->error(L("no_rank"));
					}
				}
			}else{
				isset($_GET['ajax']) ? print "error" : $this->error(L("no_record"));
			}
		}


		//创建群组
		public function creategroup(){
			isset($_GET['step']) ? $step = $_GET['step'] : $step = 1;
			switch ($step){
				case "1":
					$this->display();
					break;
				case "2":
					if(!isset($_GET['id']))
						$this->error(L("action_error"));
					$id = safeRequest($_GET['id']);
					$dao = D('groups');
					$record = $dao->find("id=".$id." and manager=".$this->userInfo['id']);
					if(!is_object($record)){
						$this->error(L("action_error"));
					}
					$this->assign('group',$record);
					$this->display('Creategroup2');
					break;
				case "3":
					if(!isset($_GET['id']))
						$this->error(L("action_error"));
					$id = safeRequest($_GET['id']);
					$dao = D('groups');
					$record = $dao->find("id=".$id." and manager=".$this->userInfo['id']);
					if($record){
						$this->error(L("action_error"));
					}
					$friendDao = D('Friends');
					$friendList = $friendDao->findAll('userid='.$this->userInfo['id']);
					$this->assign('group',$record);
					$this->assign('friendList',$friendList);
					$this->display('Creategroup3');
					break;
			}
		}

		//群组数据入库
		public function savegroup(){
			$dao = D('groups');
			$dao->name = str_filter($_POST['name']);
			$dao->desc = str_filter($_POST['desc']);
			$dao->regtime = time();
			$dao->manager = $this->userInfo['id'];
			$dao->network = str_filter($_POST['network']);
			$dao->setting = safeRequest($_POST['setting']);
			$dao->usernum = 1;
			$id = $dao->add();
			if($id>0){
				$guDao = D('groups_users');
				$guDao->groupid = $id;
				$guDao->userid = $this->userInfo['id'];
				$guDao->usertype = 2;
				$guDao->add();
				D("cache")->setField("groupCount","(groupCount+1)","userid=".$this->userInfo['id']);
				//添加动态
				if($this->feedSetting){
					addFeed($this->userInfo['id'],"groups","创建了新群组",$id);
				}
				$this->redirect('editgroupface','group','',APP_NAME,array('id'=>$id));
			}else{
				$this->error(L("add_error"));
			}
		}
		
		//保存群组头像
		public function savegroupface(){
			import("ORG.Net.UploadFile");
        	$upload = new UploadFile();
        	$upload->allowExts = array("jpg","gif","png","bmp");
        	$upload->savePath = "./Public/Uploads/group/";
        	$upload->fileExtension = true;
        	$upload->saveRule = "uniqid";
			if($upload->upload()){
				$img = $upload->getUploadFileInfo();
				$id = safeRequest($_POST['groupid']);
				$dao = D("groups");
				$dao->find('id='.$id.' and manager='.$this->userInfo['id']);
				$oldimg = $dao->img;
				$dao->img = $img[0]["savename"];
				$dao->save();
				
				if(isset($_POST["edit"])){
					@unlink($upload->savePath.$oldimg);
					$this->success(L("edit_success"));
				}else{
					$this->redirect('creategroup','group','',APP_NAME,array('step'=>3,'id'=>$id));
				}
			}else{
				$this->error($upload->error);
			}
		}
		
		//查看群组
		public function showgroup(){
			$groupid = safeRequest($_GET['id']);
			$groupDao = D('groups');
			$groupInfo = $groupDao->find($groupid); //获得群组信息
			
			$isManager = 0;
			$groupuserDao = D('groups_users');
			$groupuserInfo = $groupuserDao->find("groupid=".$groupid." and userid=".$this->userInfo['id']);
			if($groupuserInfo){
				if( 0 == $groupuserInfo['usertype']){
					$isManager = 1; //群组成员
				}elseif( 1 == $groupuserInfo['usertype']){
					$isManager = 2; //群组管理员
				}elseif( 2 == $groupuserInfo['usertype']){
					$isManager = 3; //群组创建人
				}
			}
			
			if($groupInfo['setting'] == 2 && $isManager == 0){
				$this->error(L("no_rank"));
			}else{

				if($groupInfo['setting'] ==1 && $isManager == 0){
					$groupTopicList = "noAccess";
					$isRequest = "no";
					if(D("groups_requests")->find("groupid=".$groupid." and userid=".$this->userInfo['id'],"id")){
						$isRequest = "yes";
						$this->assign("isRequest",$isRequest);
					}
				}else{
					//获得群组最新20个主题
					$groupTopicDao = D('groups_topics');
					$groupTopicList = $groupTopicDao->findAll('groupid='.$groupid,'id,title,userid,lastuserid,lastreverttime,istop,revertnum','istop desc,id desc','20');
				}
				
				if($groupInfo['setting'] > 0 && $isManager > 1){//查看是否有申请
					$requestCount = D("groups_requests")->count("groupid=".$groupid,"id");
					if($requestCount > 0){
						$this->assign("requestCount",$requestCount);
					}
				}

				$groupuserList = $groupuserDao->findAll("groupid=".$groupid,"*","",6);
				$groupManagerList = $groupuserDao->findAll("groupid=".$groupid." and usertype>0","userid,usertype");
				$this->assign("group",$groupInfo);
				$this->assign("grouptopic",$groupTopicList);
				$this->assign("groupuser",$groupuserList);
				$this->assign("groupmanager",$groupManagerList);
				$this->assign("isManager",$isManager);
				$this->display();
			}
		}

		//更新群组
		public function editgroup(){
			$groupid = safeRequest($_GET["id"]);
			if(D("groups_users")->find("groupid=".$groupid." and userid=".$this->userInfo['id']." and usertype>0")){
				$record = D("groups")->find($groupid);
				$this->assign("group",$record);
				$this->display();	
			}else{
				$this->error(L("no_rank"));
			}
			
		}

		//更新群组入库
		public function updategroup(){
			$groupid = safeRequest($_POST["id"]);
			if(D("groups_users")->find("groupid=".$groupid." and userid=".$this->userInfo['id']." and usertype>0")){
				$dao = D("groups");
				$dao->find($groupid);
				$dao->name = str_filter($_POST["name"]);
				$dao->desc = str_filter($_POST["desc"]);
				$dao->setting = safeRequest($_POST["setting"]);
				$dao->network = str_filter($_POST["network"]);
				if($dao->save()){
					isset($_POST["ajax"]) ? print "success" : $this->success(L("save_success"));
				}else{
					isset($_POST["ajax"]) ? print "error" : $this->error(L("save_error"));
				}
			}else{
				isset($_POST["ajax"]) ? print "error" : $this->error(L("no_rank"));
			}
		}

		//更新群组头像
		public function editgroupface(){
			$groupid = safeRequest($_GET["id"]);
			if(D("groups_users")->find("groupid=".$groupid." and userid=".$this->userInfo['id']." and usertype>0")){
				$record = D("groups")->find($groupid);
				$this->assign("group",$record);
				$this->display();
			}else{
				$this->error(L("no_rank"));
			}
			
		}

		//管理群组成员
		public function setgroupuser(){
			if(!isset($_GET['id']))
				$this->error(L("action_error"));
			$id = safeRequest($_GET['id']);
			$dao = D('groups');
			$record = $dao->find($id);
			$groupUserDao = D('groups_users');
			if($groupUserDao->find("groupid=".$id." and userid=".$this->userInfo['id'])){
				$friendDao = D('friends');
				$friendList = $friendDao->findAll('userid='.$this->userInfo['id']);
				$groupUserList = $groupUserDao->findAll("groupid=".$id);
				$groupRequestuser = D("groups_requests")->findAll("groupid=".$id);
				$groupInviteuser = D("groups_invites")->findAll("groupid=".$id);
				$this->assign('group',$record);
				$this->assign('friendList',$friendList);
				$this->assign('groupUserList',$groupUserList);
				$this->assign('groupRequestuser',$groupRequestuser);
				$this->assign('groupInviteuser',$groupInviteuser);
				$this->display();
			}else{
				$this->error(L("no_rank"));
			}
		}

		//设置管理员
		public function setmanager(){
			$groupid = safeRequest($_GET["gid"]);
			$userid = safeRequest($_GET["u"]);
			if(D("groups")->find("id=".$groupid." and manager=".$this->userInfo['id'])){
				if(D("groups_users")->setField("usertype",1,"groupid=".$groupid." and userid=".$userid)){
					addNotify($userid,$this->userInfo['id'],"setgroupmanager",$groupid) //添加通知
					isset($_GET["ajax"]) ? print "success" : $this->success(L("setmanager_success"));
				}else{
					isset($_GET["ajax"]) ? print "error" : $this->error(L("setmanager_error"));
				}
			}else{
					isset($_GET["ajax"]) ? print "error" : $this->error(L("no_rank"));
			}
		}

		//取消管理员
		public function cancelmanager(){
			$groupid = safeRequest($_GET["gid"]);
			$userid = safeRequest($_GET["u"]);
			if(D("groups")->find("id=".$groupid." and manager=".$this->userInfo['id'])){
				if(D("groups_users")->setField("usertype",0,"groupid=".$groupid." and userid=".$userid)){
					addNotify($userid,$this->userInfo['id'],"cancelgroupmanager",$groupid) //添加通知
					isset($_GET["ajax"]) ? print "success" : $this->success(L("cancelmanager_success"));
				}else{
					isset($_GET["ajax"]) ? print "error" : $this->error(L("cancelmanager_error"));
				}
			}else{
					isset($_GET["ajax"]) ? print "error" : $this->error(L("no_rank"));
			}
		}
		
		//删除用户
		public function deleteuser(){
			$groupid = safeRequest($_GET["gid"]);
			$userid = safeRequest($_GET["u"]);
			$dao = D("groups_users");
			if($userid != $this->userInfo['id']){
				if(D("groups")->find("id=".$groupid." and manager=".$this->userInfo['id'])){
					if($dao->delete("groupid=".$groupid." and userid=".$userid)){
						D("groups")->setField("usernum","(usernum-1)","id=".$groupid);
						D("cache")->setField("groupCount","(groupCount-1)","userid=".$userid);
						isset($_GET["ajax"]) ? print "success" : $this->success(L("deleteuser_success"));			
					}else{
						isset($_GET["ajax"]) ? print "error" : $this->error(L("deleteuser_error"));
					}
				}else{
					if($dao->find("groupid=".$groupid." and userid=".$this->userInfo['id']." and usertype=1") && $dao->find("groupid=".$groupid." and userid=".$userid." and usertype=0")){
						D("groups")->setField("usernum","(usernum-1)","id=".$groupid);
						D("cache")->setField("groupCount","(groupCount-1)","userid=".$userid);
						isset($_GET["ajax"]) ? print "success" : $this->success(L("deleteuser_success"));
					}else{
						isset($_GET["ajax"]) ? print "error" : $this->error(L("deleteuser_error"));
					}
				}
			}else{
				isset($_GET["ajax"]) ? print "error" : $this->error(L("no_delete_self"));
			}
		}


		//群组用户列表
		public function groupuser(){
			$groupid = safeRequest($_GET["id"]);
			$record = D("groups")->find("id=".$groupid,"id,name,img");
			if($record){
				import("ORG.Util.Page"); //导入分页类
				$userCount = D('groups_users')->count('groupid='.$groupid,'id');
				$listRows = C("publicListRows"); 
				$p = new Page($userCount,$listRows); //实例化分页类
				$p->setConfig('header' ,L("group_page_title")); 
				$userList = D('groups_users')->findAll('groupid='.$groupid,'*','id asc',$p->firstRow.','.$p->listRows);
				$page  = $p->show();
				$this->assign("page",$page);
				$this->assign("userlist",$userList);
				$this->assign("group",$record);
				$this->display();
			}else{
				$this->error(L("no_record"));
			}
		}

		//发送群组邀请
		public function postgroupinvite(){
			$groupid = safeRequest($_POST["groupid"]);
			$friends = $_POST['friends'];
			$arr = explode(",",$friends);
			$count = count($arr);
			$dao = D("groups_users");
			if($dao->find("groupid=".$groupid." and userid=".$this->userInfo['id']." and usertype>0")){
				for($i=0;$i<$count;$i++){
					$data[$i]["groupid"] = $groupid;
					$data[$i]["postid"] = $this->userInfo['id'];
					$data[$i]["userid"] = $arr[$i];
					$data[$i]["invitemsg"] = str_filter($_POST["invitemsg"]);
				}
				if(D("groups_invites")->addAll($data)){
					isset($_GET["ajax"]) ? print "success" : $this->success(L("inviteuser_success"));
				}else{
					isset($_GET["ajax"]) ? print "error" : $this->error(L("inviteuser_error"));
				}
			}else{
				isset($_GET["ajax"]) ? print "error" : $this->error(L("no_rank"));
			}
		}

		//接受群组邀请
		public function acceptgroupinvite(){
			$groupid = safeRequest($_GET["id"]);
			$dao = D("groups_invites");
			if($dao->find("groupid=".$groupid." and userid=".$this->userInfo['id'])){
				$groupuserDao = D("groups_users");
				$groupuserDao->groupid = $groupid;
				$groupuserDao->userid = $this->userInfo['id'];
				if($groupuserDao->add()){
					$dao->delete($dao->id);
					D("groups")->setField("usernum","(usernum+1)","id=".$groupid);
					D("cache")->setField("groupCount","(groupCount+1)","userid=".$this->userInfo['id']);
					if($this->feedSetting){
						addFeed($this->userInfo['id'],"groups","加入了群组",$groupid);
					}
					isset($_GET["ajax"]) ? print "success" : $this->success(L("add_group_success"));
				}else{
					isset($_GET["ajax"]) ? print "error" : $this->error(L("add_group_error"));
				}
			}else{
				isset($_GET["ajax"]) ? print "error" : $this->error(L("no_record"));
			}
		}

		//忽略群组邀请
		public function ignoregroupinvite(){
			$groupid = safeRequest($_GET["id"]);
			$dao = D("groups_invites");
			if($dao->find("groupid=".$groupid." and userid=".$this->userInfo['id'])){
				if($dao->delete($dao->id)){
					isset($_GET["ajax"]) ? print "success" : $this->success(L("ignore_group_success"));
				}else{
					isset($_GET["ajax"]) ? print "error" : $this->error(L("ignore_group_error"));
				}
			}else{
				isset($_GET["ajax"]) ? print "error" : $this->error(L("no_record"));
			}
		}

		

		//加入群组
		public function joingroup(){
			$groupid = safeRequest($_GET["id"]);
			$group = D("groups")->find($groupid);
			if( 2 == $group->setting ){ //私密群,只能通过邀请加入
				$this->error(L("no_rank"));
			}elseif( 1 == $group['setting'] ){ //半私密群,需通过验证才能加入
				$dao = D("groups_requests");
				$dao->groupid = $groupid;			
				$dao->userid = $this->userInfo['id'];
				$dao->managerid= $group['manager'];
				if($dao->add()){
					isset($_GET["ajax"]) ? print "isrequest" : $this->success(L("addgroup_request_success"));
				}else{ 
					isset($_GET["ajax"]) ? print "error" :	$this->error(L("addgroup_request_error"));
				}
			}elseif( 0 == $group['setting'] ){ //公开群,可以自由加入
				$dao = D("groups_users");
				if($dao->find("groupid=".$groupid." and userid=".$this->userInfo['id'])){
					isset($_GET["ajax"]) ? print "isuser" : $this->error(L("is_groupuser"));
				}else{
					$dao->groupid = $groupid;			
					$dao->userid = $this->userInfo['id'];
					if($dao->add()){
						D("cache")->setField("groupCount","(groupCount+1)","userid=".$this->userInfo['id']);
						if($this->feedSetting){
							addFeed($this->userInfo['id'],"groups","加入了群组",$groupid);
						}
						D("groups")->setField("usernum","(usernum+1)","id=".$groupid);
						isset($_GET["ajax"]) ? print "success" : $this->success(L("addgroup_success"));
					}else{
						isset($_GET["ajax"]) ? print "error" : $this->error(L("addgroup_error"));
					}
				}
			}
		}
		
		//群组请求列表
		public function getgrouprequest(){
			$groupid = safeRequest($_GET["id"]);
			if(D("groups_users")->find("groupid=".$groupid." and userid=".$this->userInfo['id']." and usertype>1")){
				$list = D("groups_requests")->findAll("groupid=".$groupid);
				$this->assign("list",$list);
				if(isset($_GET["ajax"])){
					$this->display("ggr4ajax");
				}else{
					$this->display();
				}
			}else{
				isset($_GET["ajax"]) ? print "error" : $this->error(L("no_rank"));
			}
		}

		//确认入群请求
		public function checkgrouprequest(){
			$groupid = safeRequest($_GET["id"]);
			$groupuserDao = D("groups_users");
			if( $groupuserDao->find( "groupid=".$groupid." and userid=".$this->userInfo['id']." and usertype>0" ) ){
				$u = safeRequest($_GET["u"]);
				$dao = D("groups_requests");
				if( $dao->find( "groupid=".$groupid." and userid=".$u ) ){
					$groupuserDao->groupid = $groupid;
					$groupuserDao->userid = $u;
					$groupuserDao->usertype = 0;
					if($groupuserDao->add()){
						D("cache")->setField("groupCount","(groupCount+1)","userid=".$this->userInfo['id']);
						if($this->feedSetting){
							addFeed($u,"groups","加入了群组",$groupid);
						}
						$dao->delete("groupid=".$groupid." and userid=".$u); //删除记录
						D("groups")->setField("usernum","(usernum+1)","id=".$groupid);
						isset($_GET["ajax"]) ? print "success" : $this->success(L("check_success"));
					}else{
						isset($_GET["ajax"]) ? print "error" : $this->error(L("check_error"));
					}
				}
			}else{
				isset($_GET["ajax"]) ? print "error" : $this->error(L("no_rank"));				
			}
		}
		
		//忽略入群请求
		public function ignoregrouprequest(){
			$groupid = safeRequest($_GET["id"]);
			$groupuserDao = D("groups_users");
			if( $groupuserDao->find( "groupid=".$groupid." and userid=".$this->userInfo['id']." and usertype>0" ) ){
				$u = safeRequest($_GET["u"]);
				$dao = D("groups_requests");
				if( $dao->find( "groupid=".$groupid." and userid=".$u ) ){
					if($dao->delete("groupid=".$groupid." and userid=".$u)){ //删除记录
						isset($_GET["ajax"]) ? print "success" : $this->success(L("ignore_success"));
					}else{
						isset($_GET["ajax"]) ? print "error" : $this->error(L("ignore_error"));
					}
				}
			}else{
				isset($_GET["ajax"]) ? print "error" : $this->error(L("no_rank"));
			}
		}

		//退出群组
		public function exitgroup(){
			$groupid = safeRequest($_GET['id']);
			$dao = D("groups_users");
			if($dao->delete( "groupid=".$groupid." and userid=".$this->userInfo['id']." and usertype<2" ) ){ //除群主外,都能退出群
				//添加feed
				if( $this->feedSetting ){
					D("cache")->setField("groupCount","(groupCount-1)","userid=".$this->userInfo['id']);
					addFeed($this->userInfo['id'],"groups","退出了群组",$groupid);
				}
				D("groups")->setField("usernum","(usernum-1)","id=".$groupid);
				if(isset($_GET["ajax"])){
					echo "success";
				}else{
					$this->redirect('group','group','',APP_NAME,'',3,L("exitgroup_success"));
					//$this->success("成功退出群组");
				}
			}else{
				isset($_GET["ajax"]) ? print "error" : $this->error(L("exitgroup_error"));
			}
		}

		//删除群组
		public function deletegroup(){ //删除操作最好做一下日志及相关数据备份
			$groupid = safeRequest($_GET["id"]);
			$dao = D("groups");
			if($dao->delete("id=".$groupid." and manager=".$this->userInfo['id'])){
				D("groups_topics")->delete("groupid=".$groupid); //删除该群组话题
				D("groups_topics_reverts")->delete("groupid=".$groupid); //删除该群组回复
				D("groups_users")->delete("groupid=".$groupid); //删除该群组成员
				D("groups_requests")->delete("groupid=".$groupid); //删除该群组加入请求
				//($action,$module=MODULE_NAME,$route='',$app=APP_NAME,$params=array(),$delay=0,$msg='')
				$this->redirect('group','group','',APP_NAME,'',3,L("deletegroup_success"));
			}else{
				$this->error(L("deletegroup_error"));
			}
		}
	}
?>